package Test_1109;

import Test_1106.MySingleList;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with Intellij IDEA
 * Description:
 * User: 星辰
 */
class ListNode {
      int val;
      ListNode next;
      ListNode() {};
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
     ListNode head;
    public void createList(){
        ListNode node1=new ListNode(11);
        ListNode node2=new ListNode(22);
        ListNode node3=new ListNode(33);
        ListNode node4=new ListNode(44);
        node1.next=node2;
        node2.next=node3;
        node3.next=node4;
        head=node1;
    }
    public ListNode FindKthTOTail(ListNode head,int k){
        ListNode cur=head;
        if(head==null){
            return null;
        }
        int len=0;
        while(cur!=null){
            len++;
            cur=cur.next;
        }
        if(k>len||k<0){
            return null;
        }
        cur=head;
        while(len-k!=0){
            cur=cur.next;
            len--;
        }
        return cur;
    }
  }
  class Solution {



      public static void main(String[] args) {
          ListNode listNode=new ListNode();
          ListNode node1=new ListNode(11);
          ListNode node2=new ListNode(22);
          ListNode node3=new ListNode(33);
          ListNode node4=new ListNode(44);
          node1.next=node2;
          node2.next=node3;
          node3.next=node4;
          ListNode head=node1;
          ListNode ret=listNode.FindKthTOTail(head,2);
          System.out.println(ret.val);
      }
    //翻转
      public ListNode reverseList(ListNode head) {
          if(head==null || head.next==null){
              return head;
          }
          ListNode h=head;
          ListNode cur=head.next;
          h.next=null;
          while(cur!=null){
          ListNode prev=cur.next;
          cur.next=h;
          h=cur;
          cur=prev;
          }
          head=h;
          return head;
      }
      //寻找中间节点
      public ListNode middleNode(ListNode head) {
          ListNode fast = head;
          ListNode slow = head;
          while (fast != null && fast.next != null) {
              slow = slow.next;
              fast = fast.next.next;
          }
          return slow;
      }
      //删除所有元素
      public ListNode removeElements(ListNode head, int val) {
          if(head==null){
              return head;
          }

          ListNode prev=head;
          ListNode cur=head.next;
             while(cur.next.val!=val){
                 cur=cur.next;
                 prev=prev.next;
             }
             while(cur!=null){
                 if(cur.val==val){
                     prev.next=cur.next;
                     cur=cur.next;
                 }else{
                     prev=cur;
                     cur=cur.next;
                 }
             }
          if(head.val==val){
              head=head.next;
          }
             return head;
      }


    //消失的数字
    public int missingNumber(int[] nums) {
           int len=nums.length;
           int sum=0;
        for (int i = 0; i <= len; i++) {
            sum+=i;
        }
        for (int i = 0; i < len; i++) {
            sum-=nums[i];
        }
        return sum;
    }
        public void rotate(int[] nums, int k) {
            int len=nums.length;
             int[] tmp=new int[len];
            for (int i = 0; i < len; i++) {
                tmp[i]=nums[i];
            }
            for (int i = 0; i < len; i++) {
                nums[(i+k)%len]=tmp[i];
            }
        }

    //效率太低
  /*  public int[] rotate(int[] nums, int k) {
        int m=0;
        for (int i=0; i <k ; i++) {
            int tmp=nums[nums.length-1];
            m++;
            for (int j = nums.length-2; j >=0 ; j--) {
                nums[j+1]=nums[j];
            }
            nums[0]=tmp;
        }
        return nums;
    }*/
}
public class Test {
    public static void main1(String[] args) {
        Solution solution=new Solution();
        int[] nums={0,1,3,4};
        int k=2;
        solution.rotate(nums,k);
        int ret=solution.missingNumber(nums);
        System.out.println(ret);
    }


}
